Welcome to the Beginners Guide to Setting Up and Coding in Python¶
Written by Finnagin Wantland¶
Preface:¶
In this notebook I will go over the how to get set up a coding enviornment and how to code in python on you at home device, whether it on Mac, Linux, or Windows. This guide is by no means comprehensive on how to code in python but will be a good starting point for those who are interested in learning how! My reason for writing this is to expand my own knowledge of coding and using different languages while at the same time providing a useful tool for free to those who want to begin down this journey of computer knowledge! My background in coding and computers in general comes from me currently seeking a BS in Computer Science at the University of Nevada, Las Vegas. This has quickly become a passion of mine and I hope after reading this guide you too see the fun in creating programs of your own! For any questions or inquiries you can reach me by email: fwantland50@gmail.com or my GitHub FinnWant.
Section 1: Setting up your Coding enviornment¶
1.1 Downloading Python¶
The first thing we want to do to set up our coding enviornment is to dowload your desired version of Python, make sure to click the link on the download page that corrisponds to your OS. In the most common case we have Windows but for all my Macbook and Ultrabook lovers there are links to Mac and Linux respectively. Make sure to check the box during setup to "Add Python to PATH" if on Windows, this will allow your OS to find and run Python easily later on and prevent you from having to copy and paste the entire file path into the terminal (a total pain).
Download Link: https://www.python.org/downloads/
NOTE: if you plan on using a specific library or software make sure to check dependencies and what versions of Python are supported for it.
Once you have downloaded Python onto your computer and followed the steps from the setup application we can now move onto choosing a text editor!
1.2 Choosing a text editor¶
Now that we have Python installed on our device we now get to choose which text editor we would like to use when we begin coding! The most popular and recommended choice for text editor is Visual Studio Code, but there are many other good options such as Notepad++, Atom, Sublime Text, etc. This choice is truely up to you though and take you time to weigh your options! For this general tutorial I will use VSCode to demonstrate the set up proess.
Download Link VSCode: https://code.visualstudio.com/
Once you have downloaded VS Code and changed any desired setting make sure to head to the marketplace tab on the left side of the editor and download the Python extentions (I suggest the ones released by Microsoft) and Pylance. These will give you color coded code, syntax help, and more helpful features.
1.3 Creating a Virtual Enviornment¶
Next we'll want to open our terminal, for Linux this could be Ubuntu or for Windows open Command Prompt by typing "cmd" into the search bar on the bottom left. We are going to create a virtual enviornment to code in. This step is important for many reasons, the main one is that it lets us keep projects isolated from others. As we create projects they will require different dependencies and packages but if we all just download them onto our global intallation of Pthon it will quickly become messy with random packages and can lead to package conflicts. This will allow out code to also be run on any system with any OS or hardware.
Now that our terminal is open we will type this into it:
mkdir myProject #create a directory for our preject
cd myProject # change current directory to project directory
#create Virtual Enviornment
python -m venv .venv
Then to activate the virtual environment:
#macOS / Linux
source .venv/bin/activate
#for Windows
./venv\Scripts\activate
Now while in the virtual environment lets upgrade pip (our package manager):
python -m pip install --upgrade pip
Finally we want to open our project folder in VS Code and Use Command Palette -> Python: Select interpreter to pick our virtual enviornments interpreter (translates our code into machine language for out computer to execute). Now that we have a virtual enviornment created and a project directory we can now open our text editor inside it and finish our setup! To do this we are going to want to type ".code" into the termianl and this will open VS Code inside our directory for us. With all this done we have now officially set up our coding enviornment and are ready to start coding!
Section 2: Coding in Python¶
2.1 An Overview of Python¶
Before we begin with going into detail about coding and how to write syntactically and semantically correct programs lets take a look at Python as a whole. Python is the fastest growing programming language in the world. It also has one of the largest communities of all the prgramming languages so this gives us a large selection of libraries to choose from created by other programmers all across the world! To be more specific about what Python is, it is a High Level Coding Language which means any algorithm can be expressed in this language. It is a great language to get started with because its easy to read and write in. Python is a lanugage that also doesn't require a compiler (yay for you not having to download one) because it is TRANSLATED not compiled,then executed line by line. Python is also a general purpose language so its used across a variety of fields such as Data Analysis, Automation, AI/ML, and Video Games just to name a few!
2.2 Geting Started Coding and Data Types¶
To begin lets start with creating a .py file to begin coding. With VS Code open from before we are going to look at the left panel that shows our "Codebase" or all the files we have access to and are currently working on. We are going to click on the page icon with a plus next to it to create a new file and lets name it "helloworld.py" because what tutorial would this be if we didn't have a hello world program. With the file created and named we can start writing our program! We are going to start with a very simple program to begin with to get the hang of how to code and then build on it later on. Type this into your file:
print("Hello World")
Hello World
If we click Ctrl + S and then click the play button at the top of the screen to run we will see "Hello World" is outputted to the console. Now some of you may be wondering why we had to use the double quotes inside the print function? Thats so we can print the literal String (or a type of data that consists of a sequence of characters) to the console. There are other ways to do this though, we can use Variables to save data of the appropriate type to user defined identifers aka a container for data. Here is an example:
City_Name = "Las Vegas"
print(City_Name)
Las Vegas
Here we have the identifier defined by us the user "City_Name" and we use the Assignment Operator (=) to assign the value of the variable City_Name to the string "Las Vegas". So now what if we want to print to the console a combination of a literal string we type in and a variable we can set and change whenever we like? We can now use what is called a F String. To do this we must at inside the print function print(f"{variable_name}"). Here is an example:
City_Name = "Las Vegas"
print(f"Welcome to the city of {City_Name}")
Welcome to the city of Las Vegas
Now that we have covered what strings are and how to initailize and print them we can now move on to another important data type which is the Integer. An interger is just a whole number ex. 1, 3, 78, 10000. NOTE: Deciamls are NOT integers and are a seperate data type we will get into later so 5.5 is NOT an integer. Lets start with declaring an int variable.
age = 22
print(age) #prints out 22 to console
22
Of course we are able to do basic arithmetic with integers for now lets take what we know now and write a simple program that finds the year you were born.
my_age = 21
current_year = 2025
Birth_year = current_year - my_age
print(f"The year I was born was {Birth_year}")
The year I was born was 2004